﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Toulr.Domains.Account;
using System.Text.RegularExpressions;
using Toulr.Util;

/// <summary>
/// Summary description for PageHelper
/// </summary>
public static class PageHelper
{
    static PageHelper()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    private static string GetClientIP()
    {
        string result = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        if (null == result || result == String.Empty)
        {
            result = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
        }


        if (null == result || result == String.Empty)
        {
            result = System.Web.HttpContext.Current.Request.UserHostAddress;
        }
        return result;
    }
    public static IPLocation GetIPLocationInfo()
    {
        //string ip = GetClientIP();
        //string ip = "114.246.155.26";
        string ip = "211.103.249.9"; 
        return new IpLocateHelper(System.Web.HttpContext.Current.Server.MapPath("~/app_data/qqwry.dat")).GetIPLocation(ip);
    }
    public static string ReturnUrl
    {
        get
        {
            if (System.Web.HttpContext.Current.Session[PageHelper.KEY_RETURN_URL] == null)
            {
                return string.Empty;
            }
            return System.Web.HttpContext.Current.Session[PageHelper.KEY_RETURN_URL].ToString();
        }

        set
        {
            System.Web.HttpContext.Current.Session[PageHelper.KEY_RETURN_URL] = value;
        }
    }

    public static void Logout()
    {
        System.Web.HttpContext.Current.Session.Clear();
        if (System.Web.HttpContext.Current.Response.Cookies["userinfo"] != null)
        {
            System.Web.HttpContext.Current.Response.Cookies["userinfo"].Expires = DateTime.Now.AddYears(-1);
        }
        HttpContext.Current.Response.Redirect(Url("/account/login.aspx?type=logout"));
    }
    /// <summary>
    /// Determines whether [is correct verify code] [the specified code].
    /// ignore case
    /// </summary>
    /// <param name="code">The code.</param>
    /// <returns>
    /// 	<c>true</c> if [is correct verify code] [the specified code]; otherwise, <c>false</c>.
    /// </returns>
    /// <Author>Tecky Lee</Author>
    /// <Date>2011/5/26 11:34</Date>
    public static bool IsCorrectVerifyCode(string code)
    {
        string serverCode = PageHelper.GetNewlyVerifyCode();
        if (string.IsNullOrEmpty(serverCode))
        {
            return false;
        }
        return serverCode.ToLower() == code.ToLower();
    }
    /// <summary>
    /// URLs the specified relative URL.
    /// </summary>
    /// <param name="relativeUrl">The relative URL.</param>
    /// <returns></returns>
    /// <Author>Tecky Lee</Author>
    /// <Date>2011/5/9 20:49</Date>
    public static string Url(string relativeUrl)
    {
        string url = GetBaseUrl() + (relativeUrl.StartsWith("/") ? string.Empty : "/") + relativeUrl;
        return url;
    }
    static string baseUrl = string.Empty;
    public static string GetBaseUrl()
    {
        if (baseUrl.Length == 0)
        {
            baseUrl = HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.ApplicationPath;
        }
        return baseUrl;
    }
    /// <summary>
    /// Go2s the error.
    /// </summary>
    /// <param name="errorDetailInHtmlFormat">The error detail in HTML format.</param>
    /// <param name="url">The URL.</param>
    /// <Author>Tecky Lee</Author>
    /// <Date>2011/5/24 20:26</Date>
    public static void Go2Error(string errorDetailInHtmlFormat = null)
    {
        string rawUrl = "error.aspx";
        if (HttpContext.Current.Request.PhysicalPath.Contains(rawUrl))
        {
            return;
        }
        string parms = "?";
        if (errorDetailInHtmlFormat != null)
        {
            parms += "content=" + HttpUtility.UrlEncode(errorDetailInHtmlFormat);
        }

        if (rawUrl.Length > 1)
        {
            parms += "&";
        }
        parms += "url=" + HttpUtility.UrlEncode(HttpContext.Current.Request.Url.ToString());
        if (parms.Length > 1)
        {
            rawUrl += parms;
        }
        HttpContext.Current.Response.Redirect(PageHelper.Url(rawUrl));
    }
    /// <summary>
    /// Gets the newly verify code.
    /// </summary>
    /// <returns></returns>
    /// <Author>Tecky Lee</Author>
    /// <Date>2011/5/14 21:12</Date>
    public static string GetNewlyVerifyCode()
    {
        if (System.Web.HttpContext.Current.Session[PageHelper.KEY_VERIFYCODE] == null)
        {
            return string.Empty;
        }
        return System.Web.HttpContext.Current.Session[PageHelper.KEY_VERIFYCODE].ToString();

    }
    /// <summary>
    /// Clears the newly verify code.
    /// </summary>
    /// <returns></returns>
    /// <Author>Tecky Lee</Author>
    /// <Date>2011/5/24 21:43</Date>
    public static void ClearNewlyVerifyCode()
    {
        System.Web.HttpContext.Current.Session[PageHelper.KEY_VERIFYCODE] = null;
    }
    /// <summary>
    /// Checks the email.
    /// </summary>
    /// <param name="emailAddr">The email addr.</param>
    /// <returns></returns>
    /// <Author>Tecky Lee</Author>
    /// <Date>2011/5/20 20:15</Date>
    public static bool CheckEmail(string emailAddr)
    {
        Regex cardExp = new Regex(@"^\w+@\w+(\.\w+)+(\,\w+@\w+(\.\w+)+)*$");
        return cardExp.IsMatch(emailAddr);
    }
    #region Session Const String

    public const string KEY_VERIFYCODE = "USERVERIFYCODE";

    public const string KEY_RETURN_URL = "RETURN_URL";

    /// <summary>
    /// 用户信息
    /// </summary>
    public const string CACHE_USER_INFO = "CACHE:USERINFO";

    /// <summary>
    /// 权限信息
    /// </summary>
    public const string CACHE_POPEDOMS = "CACHE:POPEDOMS";
    #endregion

    /// <summary>
    /// Gets the current user.
    /// </summary>
    /// <value>The current user.</value>
    /// <Author>Tecky Lee</Author>
    /// <Date>2011/5/10 21:39</Date>
    public static User CurrentUser
    {
        get
        {
            return System.Web.HttpContext.Current.Session[CACHE_USER_INFO] as User;
        }
        set
        {
            System.Web.HttpContext.Current.Session[CACHE_USER_INFO] = value;
        }
    }

    /// <summary>
    /// Gets a value indicating whether this instance is landed.
    /// </summary>
    /// <value><c>true</c> if this instance is landed; otherwise, <c>false</c>.</value>
    /// <Author>Tecky Lee</Author>
    /// <Date>2011/5/10 21:39</Date>
    public static bool IsLanded
    {
        get { return CurrentUser != null; }
    }
}